// prettier-ignore
import { effects } from 'dva-core/saga';
import { appId } from '../api';

let wx = window && window.wx ? window.wx : {};
const wxService = '/public/wechat/wechat_config';

function* config() {
    const data = yield effects.put.resolve({
        type: '$request/get',
        payload: {
            url:
                `${wxService}?appId=${appId}&url=` +
                encodeURIComponent(window.location.href)
        }
    });
    wx.config({
        debug: false,
        ...data,
        jsApiList: [
            'onMenuShareAppMessage', 'onMenuShareTimeline', 'chooseWXPay', 'updateAppMessageShareData', 'updateTimelineShareData',
        ],
        fail: console.error,
        complete: console.log
    });
}
//“分享给朋友”
// title: data.title, // 分享标题
// desc: data.desc, // 分享描述
// link: data.link, // 分享链接
// imgUrl: data.imgUrl // 分享图标
function* onMenuShareAppMessage(data) {
    yield config();
    wx.ready(function() {
        // data.callback();
        wx.updateAppMessageShareData(data);
    });
}

// “分享到朋友圈”
// title: data.title, // 分享标题
// link: data.link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
// imgUrl: data.imgUrl // 分享图标
function* onMenuShareTimeline(data) {
    yield config();
    wx.ready(function() {
        wx.onMenuShareTimeline(data);
    });
}

export default {
    namespace: 'wechat',
    state: {},
    effects: {
        *setShare({ payload }) {
            yield onMenuShareAppMessage(payload);
            yield onMenuShareTimeline(payload);
            payload.callback && payload.callback(); //执行提示
        }
    }
};
